package com.longrise.android.byjk.plugins.tabfirst;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.text.TextUtils;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.HomeMultipleItem;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.byjk.permission.PermissionDialog;
import com.longrise.android.byjk.permission.PermissionUtil;
import com.longrise.android.byjk.plugins.course.allcourse.AllCoursebxyxActivity;
import com.longrise.android.byjk.plugins.course.knowledge.KnowledgeActivity;
import com.longrise.android.byjk.plugins.course.mycourse.MyCourseActivity;
import com.longrise.android.byjk.plugins.course.tiku.TikuActivity;
import com.longrise.android.byjk.plugins.tabfirst.answerQuestion.AnswerDetailActivity;
import com.longrise.android.byjk.plugins.tabfirst.news.NewsDetail2Activity;
import com.longrise.android.byjk.plugins.tabfirst.qrcode.QRcodeActivity;
import com.longrise.android.byjk.plugins.tabfirst.revision.ConsultationActivity;
import com.longrise.android.byjk.plugins.publicWeb.PublicWebActivity;
import com.longrise.android.byjk.utils.Tracker_Google;

/**
 * Created by callxkj on 2017/2/23.
 * 负责首页跳转处理
 */
public class HomeViewJumpHelper {

    private static final String TAG = "HomeViewJumpHelper";

    /**
     * @param context    activity
     * @param uniqueid   中间自定义按钮的唯一id
     * @param linkedtype 跳转原生页面2001 公司内部webview2002 内部网页需要传身份证号,userid.
     *                   拼写方法如下 &cardno=xxxx&userid=xxxx
     *                   外部网页2003 外部网页不用传身份证号,userid
     * @param text       按钮下面的文字
     */
    public static void btJump(final Context context, String uniqueid, String linkedtype, String text, String linkedurl) {
        CustomerModuleJumpHelper.toJump(context, uniqueid, linkedtype, text, linkedurl, null, null);
        addGoogleEvent(uniqueid, text);
    }

    /**
     * 新手专区 保宝头条的跳转
     *
     * @param context    activity
     * @param type       HomeMultipleItem.type1 HomeMultipleItem.type2
     * @param url        如果是webview 则这个就是连接地址
     * @param text       有可能会用来当做标题
     * @param linkedtype 跳转的type 同上面的方法参数
     */
    public static void customerModuleJump(Context context, int type, String url, String text, String linkedtype, String uniqueid) {
        if ("2003".equals(linkedtype)) {
            CustomerModuleJumpHelper.publicWebJump(context, text, url);
        } else {
            customerModuleJump2(context, type, url, text, linkedtype, uniqueid);
        }
    }

    private static void customerModuleJump2(Context context, int type, String url, String text, String linkedtype, String uniqueid) {
        switch (type) {
            case HomeMultipleItem.TYPE1:
                Bundle bundle = new Bundle();
                String userid = UserInfor.getInstance().getUserid();
                url = url + "&userid=" + userid;
                bundle.putString(PublicWebActivity.URL, url);
                bundle.putString(PublicWebActivity.BACK_TYPE, PublicWebActivity.BACK_ARROW);
                jump(context, PublicWebActivity.class, bundle);
                addGoogleEvent("Newbie-zone", uniqueid, text);
                break;
            case HomeMultipleItem.TYPE2:
                if (TextUtils.isEmpty(url)) {
                    return;
                }
                Bundle bundle2 = new Bundle();
                bundle2.putString(NewsDetail2Activity.EXTRA_NEW_URL, url);
                jump(context, NewsDetail2Activity.class, bundle2);
                break;
            case HomeMultipleItem.TYPE2_1: //跳转到资讯列表页面 保宝头条
                jump(context, ConsultationActivity.class, null);
                Tracker_Google.getInstance().addGoogleScreenName("BB-headliness");
                addGoogleEvent("BB-headlines", "View-more", "查看更多");
                break;
            case HomeMultipleItem.TYPE4: //保易的跳转
                CustomerModuleJumpHelper.toJump(context, uniqueid, linkedtype, text, null, null, null);
                addGoogleEvent("review-tools", uniqueid, text);
                break;
        }
    }

    /**
     * 跳转到二维码界面
     *
     * @param context activity
     */
    public static void QRcodeJump(final Context context) {
        PermissionUtil.getInstance().requestPermission((Activity) context, PermissionUtil.Permission.CAMERA, context.getString(R.string.camera_permission), context.getString(R.string.camera_permission_hint), new PermissionUtil.PermissionCallBack() {
            @Override
            public void onSuccess() {
                try {
                    Camera camera = Camera.open();
                    if (camera == null) {
                        new PermissionDialog.Builder((Activity) context, context.getString(R.string.camera_permission_hint)).setRequestCode(PermissionUtil.Permission.CAMERA.getRequestCode()).build().show();
                    } else {
                        camera.release();
                        jump(context, QRcodeActivity.class, null);
                        addGoogleEvent("homepage", "Scan", "扫一扫");
                    }
                } catch (Exception e) {
                    new PermissionDialog.Builder((Activity) context, context.getString(R.string.camera_permission_hint)).setRequestCode(PermissionUtil.Permission.CAMERA.getRequestCode()).build().show();
                }
            }

            @Override
            public void onDenied() {
            }
        });
    }

    /**
     * 跳转到保险医学 (全部课程)
     */
    public static void toBXYX(Context context){
        Intent alintent = new Intent(context, AllCoursebxyxActivity.class);
        context.startActivity(alintent);
    }

    /**
     * 我的课程
     */
    public static void toWDKC(Context context){
        Intent wdkcintent = new Intent(context, MyCourseActivity.class);
        context.startActivity(wdkcintent);
    }

    /**
     * 知识库
     */
    public static void toZSK(Context context){
        Intent zskintent = new Intent(context, KnowledgeActivity.class);
        context.startActivity(zskintent);
    }

    public static void toTK(Context context){
        Intent tkintent = new Intent(context, TikuActivity.class);
        context.startActivity(tkintent);
    }

    /**
     * banner 的点击跳转
     *
     * @param bean 跳转链接地址
     */
    public static void BannerJump(Context context, EntityBean bean) {
        String linkedtype = bean.getString("linkedtype");
        String linkedurl = bean.getString("linkedurl");
        String urltype = bean.getString("urltype",""); //如果为null的话 switch会报空指针异常
        String title = bean.getString("title");
        String userid = UserInfor.getInstance().getUserid();
        if ("2003".equals(linkedtype)) {  //外部第三方网站 暂时不提供这个功能
            CustomerModuleJumpHelper.publicWebJump(context, title, linkedurl);
        } else {
            switch (urltype) {
                case "flhd": //费率活动
                    linkedurl = linkedurl + "&userid=" + userid;
                    Bundle bundle = new Bundle();
                    bundle.putString(PublicWebActivity.URL, linkedurl);
                    jump(context, PublicWebActivity.class, bundle);
                    break;
                case "wdxq": //问答详情
//                    Bundle wdBundle = new Bundle();
//                    wdBundle.putString(AnswerDetailActivity.EXTRA_ANSWER_URL, linkedurl);
//                    jump(context, AnswerDetailActivity.class, wdBundle);
                    break;
                case "zxxq": //资讯详情
                    Bundle zxBundle = new Bundle();
                    zxBundle.putString(NewsDetail2Activity.EXTRA_NEW_URL, linkedurl);
                    jump(context, NewsDetail2Activity.class, zxBundle);
                    break;
            }
        }
    }

    /**
     * 签到的跳转
     */
    public static void signJump(Context context) {
        CustomerModuleJumpHelper.toJump(context, "1025", null, null, null, null, null);
    }


    public static void jump(Context context, Class<?> cls, Bundle bundle) {
        Intent intent = new Intent(context, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        context.startActivity(intent);
    }

    private static void addGoogleEvent(String uniqueid, String text) {
        if (!TextUtils.isEmpty(uniqueid)) {
            addGoogleEvent("key-functions", uniqueid, text);
        }
    }

    private static void addGoogleEvent(String par1, String par2, String par3) {
        Tracker_Google.getInstance().addEvent(par1, par2, par3);
    }

}
